package com.cty.service.impl;

import com.cty.mapper.CustomerMapper;
import com.cty.pojo.Customer;
import com.cty.pojo.CustomerExample;
import com.cty.service.CustomerService;
import com.cty.util.MD5Utils;
import com.cty.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public ServerResult regist(Customer customer) {
        String newPasswordToDB = MD5Utils.formPassToDBPass(customer.getCusPwd(),"1a2b3c4d");
        System.out.println("regist service md5 password :" + newPasswordToDB);
        customer.setCusPwd(newPasswordToDB);//reset md5 password
        int rows = customerMapper.insert(customer);
        if(rows >0)
            return ServerResult.saveSuccess(null);
        return ServerResult.saveFail(null);
    }
    @Override
    public ServerResult login(Long telno, String password) {
        password = MD5Utils.formPassToDBPass(password,"1a2b3c4d");

        System.out.println("login service md5 password :"  +password);
        CustomerExample example = new CustomerExample(); // select * from customer where custTelno = ?and cust_pwd = ?
       example.createCriteria().andCusTelnoEqualTo(telno).andCusPwdEqualTo(password);

        List<Customer> customerList =  customerMapper.selectByExample(example);
        if(customerList !=null && customerList.size() >0){
            Customer customer = customerList.get(0);
            System.out.println("login  success, customer :"  +customer);
            return ServerResult.loginSuccess(customer);
        }else{
            System.out.println("login fail" );
            return ServerResult.loginFail(null);
        }


    }
}
